﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DA;
using BL;
namespace QL_KhachHang
{
    public partial class FrmThuePhong : Form
    {
        public FrmThuePhong()
        {
            InitializeComponent();
        }
        KhachHang_BL kh = new KhachHang_BL();
        DanhSachPhong_BL ds = new DanhSachPhong_BL();
        PhieuThuePhong_BL ph = new PhieuThuePhong_BL();
        private void FrmThuePhong_Load(object sender, EventArgs e)
        {
            try
            {
                cbMaKH.DataSource = kh.LayAllDSKhachHang();
                cbMaKH.DisplayMember = "MaKH";
                cbMaPhong.DataSource = ds.LayDSPhong();
                cbMaPhong.DisplayMember = "MaPhong";
                cbMaPhong.ValueMember = "MaLoai";
                lbGia.Text = ds.LayGiaPhongTheoMa(cbMaPhong.SelectedValue.ToString()).ToString();
                lbLoaiphong.Text = cbMaPhong.SelectedValue.ToString();
                cbsl.SelectedIndex = 0;
                hienthi();
            }
            catch(Exception ex)
            {
                MessageBox.Show("" + ex);
            }
        }

        private void hienthi()
        {
            listView1.Items.Clear();
            foreach (var item in ph.LayDSPhieu())
            {
                ListViewItem it = new ListViewItem(item.MaKH);
                it.SubItems.Add(item.KhachHang.TenKH);
                it.SubItems.Add(item.MaPhong);
                it.SubItems.Add(item.DanhSachPhong.DanhMucPhong.TenLoai);
                it.SubItems.Add(item.DanhSachPhong.DanhMucPhong.DonGia.ToString());
                it.SubItems.Add(item.SoLuongKhach.ToString());
                it.SubItems.Add(item.NgayDen.ToString());
                it.SubItems.Add(item.NgayDi.ToString());
                listView1.Items.Add(it);
            }
        }

        private void cbMaKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbTenKH.Text = kh.LayTenKHTheoMa(cbMaKH.Text);
        }

        private void cbMaPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lbGia.Text = ds.LayGiaPhongTheoMa(cbMaPhong.SelectedValue.ToString()).ToString();
                lbLoaiphong.Text = cbMaPhong.SelectedValue.ToString();
            }
            catch
            { }
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            PhieuThuePhong p = new PhieuThuePhong();
            p.MaKH = cbMaKH.Text;
            p.MaPhong = cbMaPhong.Text;
            p.SoLuongKhach = int.Parse(cbsl.Text);
            p.NgayDen = DateTime.Parse(dtNgayden.Text);
            p.NgayDi = DateTime.Parse(dtNgayDi.Text);
            if (ph.ThemPhieuThuePhong(p)==true)
            {
                MessageBox.Show("Lập phiếu thuê phòng thành công");
                hienthi();
            }
            else
            {
                MessageBox.Show("Lỗi!!!Vui lòng kiểm tra lại");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                cbMaKH.Text = listView1.FocusedItem.SubItems[0].Text;
                cbMaPhong.Text = listView1.FocusedItem.SubItems[2].Text;            
                cbsl.Text = listView1.FocusedItem.SubItems[5].Text;
                dtNgayden.Text = listView1.FocusedItem.SubItems[6].Text;
                dtNgayDi.Text = listView1.FocusedItem.SubItems[7].Text;
            }
            catch
            { }
        }

    }
}
